﻿///----------------------------------------------------------------
/// Copyright (C) 2008-2010 yyssoft
/// 版权所有。
///
/// 文件名：TB_BUS_TABLEDAL.cs
/// 文件功能描述：TB_BUS_TABLE数据访问类
///
///
/// 创建标识：yyssoft 2014/12/2
///
/// 修改标识：
/// 修改描述：
///
/// 修改标识：
/// 修改描述：
///---------------------------------------------------------------
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using XMK.Tadong.Model;
using XMK.Framework.DataAccess;
using System.Data.OleDb;

namespace XMK.Tadong.Data
{
    /// <summary>
    /// TB_BUS_TABLE的数据访问类
    /// </summary>
    public class TB_BUS_TABLEDAL
    {

        #region  构造函数
        
        protected DbProvider dbProvider;

        public TB_BUS_TABLEDAL(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;
        }

        #endregion  构造函数

        #region  成员方法

        /// <summary>
        /// 判断该业务下是否配置文件
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool IsExist(TB_BUS_TABLEInfo fileObj)
        {
            string OBJ_ID = fileObj.OBJ_ID == int.MinValue ? "" : fileObj.OBJ_ID.ToString();

            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT count(*) FROM TB_BUS_TABLE WHERE OBJ_ID = '" + OBJ_ID + "'");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            object obj = DbManager.ExecScalar(cmd);

            return (Convert.ToInt16(obj) != 0);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回主标识ID,失败返回"0"</returns>
        public bool AddBySql(TB_BUS_TABLEInfo model)
        {
            bool result = true;
            string sql = @" INSERT INTO TB_BUS_TABLE 
					(OBJ_ID,TABLE_NAME) 
					VALUES(:p_OBJ_ID,:p_TABLE_NAME)";

            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql,
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "OBJ_ID", "TABLE_NAME").ToArray();
                });
            //参数设值
            cmd.SetParamValues(model.OBJ_ID, model.TABLE_NAME);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            int back = DbManager.ExecNonQuery(cmd);
            if (back == 0)
                result = false;

            return result;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回"1",失败返回"0"</returns>
        public bool UpdateBySql(TB_BUS_TABLEInfo model)
        {
            bool result = true;
            if (model == null)
            {
                return false;
            }
            string sql = @"UPDATE TB_BUS_TABLE 
					SET OBJ_ID = :p_OBJ_ID,TABLE_NAME = :p_TABLE_NAME 
					WHERE ID=:p_ID";
            //新建一个数据库操作帮助类
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sql,
                //参数生成
                delegate()
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "OBJ_ID", "TABLE_NAME", "ID").ToArray();
                });
            //参数设值
            cmd.SetParamValues(model.OBJ_ID, model.TABLE_NAME,model.ID);
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            int back = DbManager.ExecNonQuery(cmd);
            if (back == 0)
                result = false;

            return result;
        }


        /// <summary>
        /// 获取所有关联信息
        /// </summary>
        /// <returns>列表</returns>
        public DataTable GetModelBySql()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT ID,OBJ_ID,TABLE_NAME 
					FROM TB_BUS_TABLE");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;

        }

        /// <summary>
        /// 获取数据库中以A开头的表
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllTable()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"SELECT * FROM USER_TABLES ");
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());

            cmd.DbCmdInfo.CommandType = CommandType.Text;
            DataTable ds = DbHelper.ExecFullDataTable(cmd.DbProvider, cmd.DbCmdInfo);
            if (ds != null)
            {
                return ds.Copy();
            }
            return null;
        }

        #endregion  成员方法

    }
}

